package com.ebt.m.customer.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ebt.m.R;
import com.ebt.m.customer.util.DisplayUtil;

public class CustomerInfoUnitWrapper extends LinearLayout{

	private static final String TAG = "wang";
	private static final int DEFAULT_TEXT_VIEW_TEXT_COLOR = 0xff000000;
	private static final int DEFAULT_TEXT_VIEW_TEXT_SIZE_SP = 16;
	
	private Context mContext;
	private String mTextViewText;
	
	private int mTextViewTextColor = -1;
	private float mTextViewTextSize = -1;
	
	private TextView customer_info_unit_textview;
	private LinearLayout customer_info_unit_container;
	private View customer_info_unit_view;

	public CustomerInfoUnitWrapper(Context context) {
		this(context, null);
	}
	
	public CustomerInfoUnitWrapper(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}
    private int mTitleTextViewWidth = 0;
	
	public CustomerInfoUnitWrapper(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		mContext = context;
		View contentView = inflate(context, R.layout.view_customer_info_unit_wrapper, this);
		
        customer_info_unit_textview = (TextView)contentView.findViewById(R.id.customer_info_unit_textview);
        customer_info_unit_container = (LinearLayout)contentView.findViewById(R.id.customer_info_unit_container);
        
		TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.customerInfoUnit);
		
		final int count = a.getIndexCount(); 
        for (int i = 0; i < count; ++i) { 
            int attr = a.getIndex(i); 
            switch (attr) { 
            case R.styleable.customerInfoUnit_customerInfoUnitText: 
            	mTextViewText = a.getString(attr);
                break; 
            case R.styleable.customerInfoUnit_customerInfoUnitTextColor:
            	mTextViewTextColor = a.getColor(attr, DEFAULT_TEXT_VIEW_TEXT_COLOR);
                break; 
            case R.styleable.customerInfoUnit_customerInfoUnitTextSize: 
            	mTextViewTextSize = a.getDimensionPixelSize(attr, DisplayUtil.sp2px(mContext, DEFAULT_TEXT_VIEW_TEXT_SIZE_SP)); 
            	break;
            case R.styleable.customerInfoUnit_customerInfoUnitTitleWidth:
                mTitleTextViewWidth = a.getDimensionPixelOffset(attr, 0);
                break;
            }
        }
        a.recycle(); 
        
        customer_info_unit_textview = (TextView)contentView.findViewById(R.id.customer_info_unit_textview);

        if(mTitleTextViewWidth > 0) {
            customer_info_unit_textview.setWidth(mTitleTextViewWidth);
        }

        if(!TextUtils.isEmpty(mTextViewText)){
        	customer_info_unit_textview.setText(mTextViewText);
        }
        
        if(customer_info_unit_textview.getVisibility() != View.VISIBLE){
        	customer_info_unit_textview.setVisibility(View.VISIBLE);
        }
        
		if(mTextViewTextColor >= 0){
			customer_info_unit_textview.setTextColor(mTextViewTextColor);
		}
		if(mTextViewTextSize >= 0){
			customer_info_unit_textview.setTextSize(DisplayUtil.px2sp(mContext, mTextViewTextSize));
		}
        
	}
	
	public void setTextViewText(String text){
		mTextViewText = text;
		if(customer_info_unit_textview != null){
			customer_info_unit_textview.setText(mTextViewText);
		}
	}
	
	public String getTextViewText(){
		return (String) customer_info_unit_textview.getText();
	}
	
	public TextView getTextView(){
		return customer_info_unit_textview;
	}
	
	public View getContainerChildAt(int index){
		return customer_info_unit_container.getChildAt(index);
	}
	
	public LinearLayout getContainer(){
		return customer_info_unit_container;
	}
	
}
